home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / ugly / utime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-02  |  4.4 KB  |  179 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1993-1997  Thomas Aglassinger
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. /*
  21.  * ugly/utime.c
  22.  *
  23.  * misc. time-handling functions
  24.  *
  25.  * Copyright (C) 1994,95,96  Thomas Aglassinger
  26.  *
  27.  * This program is free software; you can redistribute it and/or modify
  28.  * it under the terms of the GNU General Public License as published by
  29.  * the Free Software Foundation; either version 2 of the License, or
  30.  * (at your option) any later version.
  31.  *
  32.  * This program is distributed in the hope that it will be useful,
  33.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  35.  * GNU General Public License for more details.
  36.  *
  37.  * You should have received a copy of the GNU General Public License
  38.  * along with this program; if not, write to the Free Software
  39.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  40.  *
  41.  * updated:  6-Dec-1996
  42.  * created:  6-Jun-1996
  43.  *
  44.  *-------------------------------------------------------------------
  45.  *
  46.  */
  47.  
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include <time.h>
  52.  
  53. #include "utypes.h"
  54. #include "umemory.h"
  55. #include "ustring.h"
  56. #include "expstr.h"
  57.  
  58. #define NOEXTERN_UGLY_UTIME_H
  59. #include "utime.h"
  60.  
  61. /* debugging defines */
  62. #ifdef D
  63. #undef D
  64. #endif
  65.  
  66. #if DEBUG_UGLY_TIME
  67. #define D(x) x
  68. #define DUT "*utime * "
  69. #else
  70. #define D(x)                    /* nufin */
  71. #endif
  72.  
  73. BOOL time2estr(EXPSTR * dest, struct tm *time)
  74. {
  75. #define TIMEBUF_INC_STR "12345678901234567890"
  76.  
  77.     /*localtime(&(hp->start_time)) ); */
  78.     BOOL ok = TRUE;
  79.     size_t strftrc = 0;
  80.  
  81.     /* reset destination string */
  82.     set_estr(dest, TIMEBUF_INC_STR);
  83.  
  84.     /* write time to string */
  85.     while (ok && !strftrc)
  86.     {
  87.         /* expand dest */
  88.         ok = app_estr(dest, TIMEBUF_INC_STR);
  89.  
  90.         D(fprintf(stderr, DUT "dest: inc+%ld\n",
  91.                   strlen(TIMEBUF_INC_STR)));
  92.  
  93.         /* write string */
  94.         strftrc = strftime(estr2str(dest), estrlen(dest),
  95.                            UGLY_TIME_TEMPLATE_STRFTIME, time);
  96.     }
  97.  
  98.     D(if (!ok) fprintf(stderr, DUT "time2estr: FAILED\n"));
  99.  
  100.     return (ok);
  101. }
  102.  
  103. VOID clrtime(struct tm * time)
  104. {
  105.     time->tm_sec = 0;
  106.     time->tm_min = 0;
  107.     time->tm_hour = 0;
  108.     time->tm_mday = 0;
  109.     time->tm_mon = 0;
  110.     time->tm_year = 0;
  111.     time->tm_wday = 0;
  112.     time->tm_yday = 0;
  113.     time->tm_isdst = 0;
  114. }
  115.  
  116. BOOL str2time(struct tm *time, STRPTR timestr)
  117. {
  118.     BOOL ok = FALSE;
  119.     int scanrc = EOF;
  120.  
  121.     printf("testing ugly time funtions\n");
  122.  
  123.     clrtime(time);
  124.  
  125.     /* convert string to time */
  126.     scanrc = sscanf(timestr, UGLY_TIME_TEMPLATE_SSCANF,
  127.                     &time->tm_year, &time->tm_mon, &time->tm_mday,
  128.                     &time->tm_hour, &time->tm_min, &time->tm_sec);
  129.  
  130.     if (scanrc != EOF)
  131.     {
  132.         time_t ttime;
  133.  
  134.         /* adjust values */
  135.         time->tm_year -= 1990;
  136.         time->tm_mon--;
  137.         time->tm_mday--;
  138.  
  139.         /* fill rest of time-structure */
  140.         ttime = mktime(time);
  141.  
  142.         if (ttime != ((time_t) - 1))
  143.         {
  144.  
  145.             struct tm *ltime = localtime(&ttime);
  146.  
  147.             D(
  148.                  {
  149.                  STRARR timebuf2[200];
  150.                  strftime(timebuf2, 200, UGLY_TIME_TEMPLATE_STRFTIME, ltime);
  151.                  fprintf(stderr, DUT "ltime = `%s'\n", timebuf2);
  152.                  }
  153.             );
  154.  
  155.             if (ltime)
  156.             {
  157.                 memcpy(time, ltime, sizeof(struct tm));
  158.                 ok = TRUE;
  159.             }
  160.             else
  161.             {
  162.                 D(fprintf(stderr, DUT "str2time: localtime() FAILED\n"));
  163.             }
  164.  
  165.         }
  166.         else
  167.         {
  168.             D(fprintf(stderr, DUT "str2time: mktime() FAILED\n"));
  169.         }
  170.     }
  171.     else
  172.     {
  173.         D(fprintf(stderr, DUT "str2time: sscanf() FAILED\n"));
  174.     }
  175.  
  176.     return (ok);
  177. }
  178.  
  179.